﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-12-10 12:14:26
// 负责人：Sushee
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
namespace OF.Component.DAL
{
	#region  数据访问层
	/// <summary>
	/// 数据访问层
	/// </summary>	
	internal partial class BaseLocation:IBaseLocation
	{		
    
            //连接字符串
            public virtual string ConnectionStringKey
            {
                get
                {
                    return ConstantUtility.CGTConnectionString;
                }
            }
        #region GetMaxID
        
    
      
            public int GetMaxID()
            {
                int nReturn=0;
                string szSql=@"SELECT ISNULL(MAX(LocationID),0)+1
                               FROM [BaseLocation]";             
                Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
                DbCommand dbc = db.GetSqlStringCommand(szSql);
                try
                {
                    object o = db.ExecuteScalar(dbc);
                    if (o!=null)
                    {
                        nReturn = Convert.ToInt32(o);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return nReturn;               
            }
            
      
        
        #endregion
    
		#region Basic CRUD functions
         private BaseLocationInfo GetByReader(IDataReader dr)
         {
            BaseLocationInfo info = new BaseLocationInfo();
            object obj;
            obj=dr["LocationID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.LocationID = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["LocationParentID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.LocationParentID = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["LocationName"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.LocationName = obj.ToString();
            }
            obj=dr["LocationRemark"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.LocationRemark = obj.ToString();
            }
            obj=dr["LocationStatus"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.LocationStatus = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["LocationGeographyTypeID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.LocationGeographyTypeID = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["LocationShortName"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.LocationShortName = obj.ToString();
            }
            obj=dr["LocationSpellName"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.LocationSpellName = obj.ToString();
            }
            return info;
         }
		public int Insert(BaseLocationInfo info)
		{
        
        
            string strSql=@"INSERT INTO BaseLocation			
			(			
			[LocationID],
			[LocationParentID],
			[LocationName],
			[LocationRemark],
			[LocationStatus],
			[LocationGeographyTypeID],
			[LocationShortName],
			[LocationSpellName]
			)
			 VALUES
			(			
			@LocationID,
			@LocationParentID,
			@LocationName,
			@LocationRemark,
			@LocationStatus,
			@LocationGeographyTypeID,
			@LocationShortName,
			@LocationSpellName
			)";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddInParameter(dbc, "@LocationID",DbType.Int32, info.LocationID);
            db.AddInParameter(dbc, "@LocationParentID",DbType.Int32, info.LocationParentID);
            db.AddInParameter(dbc, "@LocationName",DbType.String, info.LocationName);
            db.AddInParameter(dbc, "@LocationRemark",DbType.String, info.LocationRemark);
            db.AddInParameter(dbc, "@LocationStatus",DbType.Int32, info.LocationStatus);
            db.AddInParameter(dbc, "@LocationGeographyTypeID",DbType.Int32, info.LocationGeographyTypeID);
            db.AddInParameter(dbc, "@LocationShortName",DbType.String, info.LocationShortName);
            db.AddInParameter(dbc, "@LocationSpellName",DbType.String, info.LocationSpellName);

             
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
            return (int)db.GetParameterValue(dbc,"LocationID");
         
		}
		
		
        public void Update(BaseLocationInfo info)
		{
            StringBuilder sbSql=new StringBuilder("UPDATE BaseLocation SET ");
            sbSql.AppendLine("[LocationParentID] = @LocationParentID,");
                
            sbSql.AppendLine("[LocationName] = @LocationName,");
                
            sbSql.AppendLine("[LocationRemark] = @LocationRemark,");
                
            sbSql.AppendLine("[LocationStatus] = @LocationStatus,");
                
            sbSql.AppendLine("[LocationGeographyTypeID] = @LocationGeographyTypeID,");
                
            sbSql.AppendLine("[LocationShortName] = @LocationShortName,");
                
            sbSql.AppendLine("[LocationSpellName] = @LocationSpellName");
                
            sbSql.Append(" where  [LocationID] = @LocationID");
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(sbSql.ToString());
            db.AddInParameter(dbc, "@LocationID",DbType.Int32, info.LocationID);
            db.AddInParameter(dbc, "@LocationParentID",DbType.Int32, info.LocationParentID);
            db.AddInParameter(dbc, "@LocationName",DbType.String, info.LocationName);
            db.AddInParameter(dbc, "@LocationRemark",DbType.String, info.LocationRemark);
            db.AddInParameter(dbc, "@LocationStatus",DbType.Int32, info.LocationStatus);
            db.AddInParameter(dbc, "@LocationGeographyTypeID",DbType.Int32, info.LocationGeographyTypeID);
            db.AddInParameter(dbc, "@LocationShortName",DbType.String, info.LocationShortName);
            db.AddInParameter(dbc, "@LocationSpellName",DbType.String, info.LocationSpellName);
			
           
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
		}
		
        public void Delete(int locationID)
		{
			StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM [BaseLocation]  ");
            strSql.Append("WHERE [LocationID]=@LocationID ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@LocationID",DbType.Int32, locationID);
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
          
           
		}
        
         public IList<BaseLocationInfo> Search(out int pageCount, int pageIndex, int pageSize, string where, string orderField, bool isDesc)
        {
            IList<BaseLocationInfo> list = new List<BaseLocationInfo>();
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("sp_OF_Page");
            //设置参数
            db.AddOutParameter(dbc, "@pageCount", DbType.Int32, 4);
            db.AddInParameter(dbc, "@pageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbc, "@pageSize", DbType.Int32, pageSize);
            db.AddInParameter(dbc, "@strWhere", DbType.String, where);
            db.AddInParameter(dbc, "@fldName", DbType.String, orderField);
            db.AddInParameter(dbc, "@OrderType", DbType.Boolean, isDesc);
            db.AddInParameter(dbc, "@tblName", DbType.String, "BaseLocation");
            db.AddInParameter(dbc, "@strGetFields", DbType.String, "*");
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    BaseLocationInfo info = GetByReader(dr);
                    list.Add(info);
                }
            }
            pageCount = (int)db.GetParameterValue(dbc, "@PageCount");
            return list;
        }
        
      
        public BaseLocationInfo GetByID(int locationID)
		{
            Model.BaseLocationInfo  info = null;
		    StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  * FROM [BaseLocation] ");
            strSql.Append("WHERE [LocationID]=@LocationID ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@LocationID",DbType.Int32, locationID);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
		}
		
        
		#endregion
	}
	#endregion
}